package com.tdk.wifidirectserver.ui.nsd

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.tdk.wifidirectserver.R
import com.tdk.wifidirectserver.databinding.FragmentNsdServerBinding
import com.tdk.wifidirectserver.ui.main.ConnectedServerFragment
import com.tdk.wifidirectserver.ui.main.ConnectedServerViewModel
import com.tdk.wifidirectserver.ui.chat.ChatFragment
import com.tdk.wifidirectserver.ui.main.ConnectedClientViewModel

class NsdServerFragment : Fragment() {
    lateinit var binding: FragmentNsdServerBinding
    lateinit var connectedServerViewModel: ConnectedServerViewModel


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        connectedServerViewModel = ViewModelProvider(this)[ConnectedServerViewModel::class.java]
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentNsdServerBinding.inflate(layoutInflater, container, false)
        initView()
        return binding.root
    }

    private fun initView() {

        connectedServerViewModel.serverCreateStatus.observe(viewLifecycleOwner) {
            Log.d(TAG, "socketConnectStatus: $it")
            if (it) {
                binding.btCloseServer.isEnabled = true
                binding.btStartServer.isEnabled = false
            } else {
                binding.btStartServer.isEnabled = true
                binding.btCloseServer.isEnabled = false
            }
        }

        connectedServerViewModel.socketConnectStatus.observe(viewLifecycleOwner) {

            if (it) {
                activity?.supportFragmentManager?.beginTransaction()
                    ?.replace(
                        R.id.fragment_container,
                        ChatFragment.newInstance(true, "", connectedServerViewModel),
                        ChatFragment::class.java.simpleName
                    )
                    ?.commit()
                binding.btStartServer.isEnabled = false
                binding.btCloseServer.isEnabled = true
            } else {
                val chatFragment =
                    activity?.supportFragmentManager?.findFragmentByTag(ChatFragment::class.java.simpleName)
                if (chatFragment != null) {
                    activity?.supportFragmentManager?.beginTransaction()?.remove(chatFragment)?.commit()
                }
                binding.btStartServer.isEnabled = true
                binding.btCloseServer.isEnabled = false
            }
        }
        binding.btStartServer.setOnClickListener {
            connectedServerViewModel.startServer()
        }

        binding.btCloseServer.setOnClickListener {
            connectedServerViewModel.closeServer()
        }


        binding.btStartNsd.setOnClickListener {
            connectedServerViewModel.startNsd()
        }

        binding.btStopNsd.setOnClickListener {
            connectedServerViewModel.stopNsd()
        }

    }

    companion object {
        @JvmStatic
        fun newInstance(viewModel: ConnectedServerViewModel) =
            NsdServerFragment().apply {
                connectedServerViewModel = viewModel
            }

        private const val TAG = "ConnectedServerFragment"
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
    }
}